﻿using Model;
using Model.medical;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class MedicationDAL
    {
        public MedicationDAL(HoteContext hoteContext)
        {
            HoteContext = hoteContext;
        }

        public HoteContext HoteContext { get; }
        /// <summary>
        /// 用药添加
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public int MedicaAdd(MedicationModel info)
        {
            try
            {
                var res = HoteContext.MedicaYongyao.Count(o => o.IsDelete == false && o.MedicalName == info.MedicalName);
                if (res > 0)
                {
                    return -1;//科室名称不能重复
                }
                HoteContext.MedicaYongyao.Add(info);
                return HoteContext.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 显示查询分页
        /// </summary>
        /// <param name="totalCount"></param>
        /// <param name="totalPage"></param>
        /// <param name="Pageindex"></param>
        /// <param name="pageSize"></param>
        /// <param name="Dispensingdoctors"></param>
        /// <param name="DepartId"></param>
        /// <returns></returns>
        public IQueryable<Medication> GetMedicatioList(out int totalCount,out int totalPage,int Pageindex=1,int pageSize=2,string? Dispensingdoctors="",int? DepartId=null)
        {
            try
            {
                var list = HoteContext.MedicaYongyao.Join(HoteContext.DepKeshi, a => a.DepartId, b => b.Id, (a, b) => new Medication
                {
                  Id=a.Id,

                    IsDelete=a.IsDelete,
                    MedicalName=a.MedicalName,
                    patientName=a.patientName,
                    patientNumber=a.patientNumber,
                    State=a.State,
                    Createdtime=a.Createdtime,
                    DepartId=a.DepartId,
                    Updatedtime=a.Updatedtime,
                    Dispensingdoctors=a.Dispensingdoctors,
                    Medicatime=a.Medicatime,
                    Department=b.Department,
                }).Where(o => o.IsDelete == false);
                if (!string.IsNullOrEmpty(Dispensingdoctors))
                {
                    list = list.Where(o => o.Dispensingdoctors.Contains(Dispensingdoctors));
                }
                if (DepartId!=null)
                {
                    list = list.Where(o => o.DepartId == DepartId);
                }
                totalCount = list.Count();
                totalPage = (int)Math.Ceiling(totalCount * 1.0 / pageSize);
                list = list.OrderByDescending(o => o.Id).Skip((Pageindex - 1) * pageSize).Take(pageSize);
                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// Id查询反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public MedicationModel GetMedicationId(int id)
        {
            try
            {
                return HoteContext.MedicaYongyao.Find(id);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 逻辑删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int MedicaDelete(int id)
        {
            try
            {
                var res = HoteContext.MedicaYongyao.Find(id);
                if (res != null)
                {
                    res.IsDelete = true;
                }
                return HoteContext.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 批量修改状态
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public int MedicaUptAll(string ids,int state)
        {
            try
            {
                var idsAll = ids.Split(',');
                foreach (var id in idsAll)
                {
                    var info = HoteContext.MedicaYongyao.Find(Convert.ToInt32(id));
                    info.State=state;
                }
                return HoteContext.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public int MedicaUpt(MedicationModel info)
        {
            try
            {
                var res = HoteContext.MedicaYongyao.Count(o => o.IsDelete == false && o.MedicalName == info.MedicalName && o.Id != info.Id);
                if (res > 0)
                {
                    return -1;//名称已存在
                }
                HoteContext.MedicaYongyao.Update(info);
                return HoteContext.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
